
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <title>hydrate · React源码解析</title>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.0">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-api/theme-api.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

         
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="ref.html" />
    
    
    <link rel="prev" href="context.html" />
    

<script
  async
  src="https://www.googletagmanager.com/gtag/js?id=UA-49483742-5"
></script>
<script>
  window.dataLayer = window.dataLayer || []
  function gtag() {
    dataLayer.push(arguments)
  }
  gtag('js', new Date())

  gtag('config', 'UA-49483742-5')
</script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    首页
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    基础
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../api/react.html">
            
                <a href="../api/react.html">
            
                    
                    React API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../api/react-element.html">
            
                <a href="../api/react-element.html">
            
                    
                    ReactElement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../api/react-children.html">
            
                <a href="../api/react-children.html">
            
                    
                    React Children
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../api/react-structure.html">
            
                <a href="../api/react-structure.html">
            
                    
                    React 中的数据结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    创建更新
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../update/react-dom-render.html">
            
                <a href="../update/react-dom-render.html">
            
                    
                    ReactDOM.render
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../update/set-state.html">
            
                <a href="../update/set-state.html">
            
                    
                    setState & forceUpdate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../update/expiration-time.html">
            
                <a href="../update/expiration-time.html">
            
                    
                    expirationTime 计算
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    任务调度
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../flow/scheduler-global.html">
            
                <a href="../flow/scheduler-global.html">
            
                    
                    全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../flow/schedule-work.html">
            
                <a href="../flow/schedule-work.html">
            
                    
                    scheduleWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../flow/scheduler-pkg.html">
            
                <a href="../flow/scheduler-pkg.html">
            
                    
                    react-scheduler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../flow/perform-work.html">
            
                <a href="../flow/perform-work.html">
            
                    
                    performWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../flow/perform-unit-of-work.html">
            
                <a href="../flow/perform-unit-of-work.html">
            
                    
                    performUnitOfWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../flow/render-root.html">
            
                <a href="../flow/render-root.html">
            
                    
                    renderRoot
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.6.1" data-path="../flow/render-root/throw-exception.html">
            
                <a href="../flow/render-root/throw-exception.html">
            
                    
                    throwException
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../flow/begin-work.html">
            
                <a href="../flow/begin-work.html">
            
                    
                    beginWork
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.7.1" data-path="../flow/begin-work/mount-indeterminate-component.html">
            
                <a href="../flow/begin-work/mount-indeterminate-component.html">
            
                    
                    mountIndeterminateComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.2" data-path="../flow/begin-work/mount-lazy-component.html">
            
                <a href="../flow/begin-work/mount-lazy-component.html">
            
                    
                    mountLazyComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.3" data-path="../flow/begin-work/update-functional-component.html">
            
                <a href="../flow/begin-work/update-functional-component.html">
            
                    
                    updateFunctionalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.4" data-path="../flow/begin-work/update-class-component.html">
            
                <a href="../flow/begin-work/update-class-component.html">
            
                    
                    updateClassComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.5" data-path="../flow/begin-work/update-host-root.html">
            
                <a href="../flow/begin-work/update-host-root.html">
            
                    
                    updateHostRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.6" data-path="../flow/begin-work/update-host-component.html">
            
                <a href="../flow/begin-work/update-host-component.html">
            
                    
                    updateHostComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.7" data-path="../flow/begin-work/update-host-text.html">
            
                <a href="../flow/begin-work/update-host-text.html">
            
                    
                    updateHostText
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.8" data-path="../feature/Suspense-component.md">
            
                <span>
            
                    
                    updateSuspenseComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.9" data-path="../flow/begin-work/update-portal-component.html">
            
                <a href="../flow/begin-work/update-portal-component.html">
            
                    
                    updatePortalComponent
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.10" data-path="../flow/begin-work/update-forward-ref.html">
            
                <a href="../flow/begin-work/update-forward-ref.html">
            
                    
                    updateForwardRef
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.11" data-path="../flow/begin-work/update-fragment.html">
            
                <a href="../flow/begin-work/update-fragment.html">
            
                    
                    updateFragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.12" data-path="../flow/begin-work/update-mode.html">
            
                <a href="../flow/begin-work/update-mode.html">
            
                    
                    updateMode
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.13" data-path="../flow/begin-work/update-profiler.html">
            
                <a href="../flow/begin-work/update-profiler.html">
            
                    
                    updateProfiler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.14" data-path="../flow/begin-work/update-context-provider.html">
            
                <a href="../flow/begin-work/update-context-provider.html">
            
                    
                    updateContextProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7.15" data-path="../flow/begin-work/update-context-consumer.html">
            
                <a href="../flow/begin-work/update-context-consumer.html">
            
                    
                    updateContextConsumer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../flow/reconcile-children.html">
            
                <a href="../flow/reconcile-children.html">
            
                    
                    reconcileChildren
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.8.1" data-path="../flow/reconcile-children/single.html">
            
                <a href="../flow/reconcile-children/single.html">
            
                    
                    调和单个节点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8.2" data-path="../flow/reconcile-children/array.html">
            
                <a href="../flow/reconcile-children/array.html">
            
                    
                    调和列表
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    commit 阶段
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../commit-phase/commit-root.html">
            
                <a href="../commit-phase/commit-root.html">
            
                    
                    commitRoot
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../commit-phase/invokeGuardedCallback.html">
            
                <a href="../commit-phase/invokeGuardedCallback.html">
            
                    
                    invokeGuardedCallback
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../commit-phase/commitBeforeMutationLifecycles.html">
            
                <a href="../commit-phase/commitBeforeMutationLifecycles.html">
            
                    
                    commitBeforeMutationLifecycles
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../commit-phase/commit-all-host-effects.html">
            
                <a href="../commit-phase/commit-all-host-effects.html">
            
                    
                    commitAllHostEffects
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.4.1" data-path="../commit-phase/host-effects/CommitPlacement.html">
            
                <a href="../commit-phase/host-effects/CommitPlacement.html">
            
                    
                    commitPlacement
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.2" data-path="../commit-phase/host-effects/CommitWork.html">
            
                <a href="../commit-phase/host-effects/CommitWork.html">
            
                    
                    commitWork
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4.3" data-path="../commit-phase/host-effects/CommitDeletion.html">
            
                <a href="../commit-phase/host-effects/CommitDeletion.html">
            
                    
                    commitDeletion
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../commit-phase/commit-all-lifecycles.html">
            
                <a href="../commit-phase/commit-all-lifecycles.html">
            
                    
                    commitAllLifecycle
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    功能
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="context.html">
            
                <a href="context.html">
            
                    
                    context
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.2" data-path="hydrate.html">
            
                <a href="hydrate.html">
            
                    
                    hydrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="ref.html">
            
                <a href="ref.html">
            
                    
                    ref
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="event-injection.html">
            
                <a href="event-injection.html">
            
                    
                    事件系统初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="event-bind.html">
            
                <a href="event-bind.html">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="event-dispatch.html">
            
                <a href="event-dispatch.html">
            
                    
                    事件触发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="event-create-event-object.html">
            
                <a href="event-create-event-object.html">
            
                    
                    事件对象生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="Suspense-component.html">
            
                <a href="Suspense-component.html">
            
                    
                    Suspense
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="suspense.html">
            
                <a href="suspense.html">
            
                    
                    挂起任务的含义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="suspense-lazy.html">
            
                <a href="suspense-lazy.html">
            
                    
                    lazy 组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Hooks
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../hooks/hooks-start.html">
            
                <a href="../hooks/hooks-start.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../hooks/hooks-use-state.html">
            
                <a href="../hooks/hooks-use-state.html">
            
                    
                    useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../hooks/hooks-use-effect.html">
            
                <a href="../hooks/hooks-use-effect.html">
            
                    
                    useEffect
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../hooks/hooks-others.html">
            
                <a href="../hooks/hooks-others.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../hooks/hooks-common.html">
            
                <a href="../hooks/hooks-common.html">
            
                    
                    通用API
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <form
            action="https://buttondown.email/api/emails/embed-subscribe/Jokcy"
            method="post"
            target="popupwindow"
            onsubmit="window.open('https://buttondown.email/Jokcy', 'popupwindow')"
            class="buttondown-form"
            >
            <input type="email" name="email" id="bd-email" placeholder="(your@email.com)">
            <input type="hidden" value="1" name="embed"/>
            <button type="submit">订阅我</button>
        </form>
    </li>
</ul>

                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                 
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >hydrate</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="hydrate">hydrate</h1><p><code>hydrate</code>&#x662F; React &#x4E2D;&#x63D0;&#x4F9B;&#x5728;&#x521D;&#x6B21;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53BB;&#x590D;&#x7528;&#x539F;&#x672C;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684; DOM &#x8282;&#x70B9;&#xFF0C;&#x51CF;&#x5C11;&#x91CD;&#x65B0;&#x751F;&#x6210;&#x8282;&#x70B9;&#x4EE5;&#x53CA;&#x5220;&#x9664;&#x539F;&#x672C; DOM &#x8282;&#x70B9;&#x7684;&#x5F00;&#x9500;&#xFF0C;&#x6765;&#x52A0;&#x901F;&#x521D;&#x6B21;&#x6E32;&#x67D3;&#x7684;&#x529F;&#x80FD;&#x3002;&#x4E3B;&#x8981;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x662F;<strong>&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x6216;&#x8005;&#x50CF;<code>prerender</code>&#x7B49;&#x60C5;&#x51B5;</strong>&#x3002;</p><p>&#x51B3;&#x5B9A;&#x5728;&#x521D;&#x6B21;&#x6E32;&#x67D3;&#x65F6;&#x662F;&#x5426;&#x9700;&#x8981;&#x6267;&#x884C;<code>hydrate</code>&#x7684;&#x6807;&#x51C6;&#x53D6;&#x51B3;&#x4E8E;&#x4EE5;&#x4E0B;&#x6761;&#x4EF6;</p><h4 id="&#x662F;&#x5426;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x65F6;&#x5C31;&#x6307;&#x5B9A;&#x4F7F;&#x7528;hydrate">&#x662F;&#x5426;&#x8C03;&#x7528;&#x65B9;&#x6CD5;&#x65F6;&#x5C31;&#x6307;&#x5B9A;&#x4F7F;&#x7528;<code>hydrate</code></h4><p>&#x521D;&#x6B21;&#x6E32;&#x67D3;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;&#xFF1A;<code>ReactDOM.render</code>&#x548C;<code>ReactDOM.hydrate</code>&#x3002;&#x540E;&#x8005;&#x5C31;&#x662F;&#x76F4;&#x63A5;&#x544A;&#x8BC9;<code>ReactDOM</code>&#x9700;&#x8981;<code>hydrate</code>&#xFF0C;<strong>&#x76EE;&#x524D;&#x6765;&#x8BF4;&#x5982;&#x679C;&#x4F60;&#x8C03;&#x7528;&#x7684;&#x662F;<code>render</code>&#xFF0C;&#x4F46;&#x662F; React &#x4F1A;&#x68C0;&#x6D4B;&#x662F;&#x5426;&#x53EF;&#x4EE5;<code>hydrate</code>&#xFF0C;&#x5982;&#x679C;&#x53EF;&#x4EE5;&#x4ED6;&#x4F1A;&#x63D0;&#x9192;&#x4F60;&#x5E94;&#x8BE5;&#x4F7F;&#x7528;<code>hydrate</code>&#x3002;</strong></p><h4 id="shouldhydrateduetolegacyheuristic">shouldHydrateDueToLegacyHeuristic</h4><p>&#x5373;&#x4FBF;&#x4F60;&#x4E0D;&#x6307;&#x5B9A;&#x9700;&#x8981;<code>hydrate</code>&#xFF0C;React &#x4E5F;&#x4F1A;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x68C0;&#x6D4B;</p><pre><code class="lang-js"><span class="hljs-keyword">const</span> ROOT_ATTRIBUTE_NAME = <span class="hljs-string">&apos;data-reactroot&apos;</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">shouldHydrateDueToLegacyHeuristic</span>(<span class="hljs-params">container</span>) </span>{
  <span class="hljs-keyword">const</span> rootElement = getReactRootElementInContainer(container)
  <span class="hljs-keyword">return</span> !!(
    rootElement &amp;&amp;
    rootElement.nodeType === ELEMENT_NODE &amp;&amp;
    rootElement.hasAttribute(ROOT_ATTRIBUTE_NAME)
  )
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getReactRootElementInContainer</span>(<span class="hljs-params">container: any</span>) </span>{
  <span class="hljs-keyword">if</span> (!container) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
  }

  <span class="hljs-keyword">if</span> (container.nodeType === DOCUMENT_NODE) {
    <span class="hljs-keyword">return</span> container.documentElement
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> container.firstChild
  }
}</code></pre>
<p>&#x5982;&#x679C;<code>container</code>&#x662F;<code>document</code>&#x5219;<code>rootElement</code>&#x662F;<code>html</code>&#xFF0C;&#x5426;&#x5219;&#x662F;&#x4ED6;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x5B50;&#x8282;&#x70B9;&#x3002;&#x770B;&#x5230;&#x8FD9;&#x91CC;&#x5C31;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x6211;&#x4EEC;&#x4E3A;&#x4EC0;&#x4E48;&#x4E0D;&#x63A8;&#x8350;&#x4F7F;&#x7528;<code>document</code>&#x6765;&#x4F5C;&#x4E3A;<code>container</code>&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x4ED6;&#x4F1A;&#x76F4;&#x63A5;&#x628A;<code>html</code>&#x8986;&#x76D6;&#x3002;</p><p>&#x7136;&#x540E;&#x5224;&#x65AD;&#x6761;&#x4EF6;&#x662F;<code>rootElement</code>&#x662F;&#x666E;&#x901A;&#x7684;<code>element</code>&#x5E76;&#x4E14;&#x5177;&#x6709;<code>data-reactroot</code>&#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x662F; React &#x5728;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#x52A0;&#x4E0A;&#x53BB;&#x7684;&#x3002;</p><hr><p>&#x7B26;&#x5408;&#x4EE5;&#x4E0A;&#x4E24;&#x4E2A;&#x6761;&#x4EF6;&#x4E4B;&#x4E00;&#x7684;&#xFF0C;&#x5C31;&#x4F1A;&#x5728;&#x521D;&#x6B21;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#x8FDB;&#x884C;<code>hydrate</code>&#xFF0C;&#x901A;&#x8FC7;&#x5728;<code>FiberRoot</code>&#x4E0A;&#x6807;&#x8BB0;<code>hydrate</code>&#x5C5E;&#x6027;&#x3002;</p><h1 id="&#x5F00;&#x59CB;hydrate">&#x5F00;&#x59CB;<code>hydrate</code></h1><p>&#x5728;&#x66F4;&#x65B0;&#x5230;<code>HostRoot</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x6B63;&#x5F0F;&#x5F00;&#x59CB;&#x4E86;<code>hydrate</code>&#x7684;&#x6D41;&#x7A0B;&#xFF0C;<code>hydrate</code>&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x4F53;&#x7684;&#x6A21;&#x5757;&#x8BBE;&#x8BA1;&#xFF0C;&#x4F1A;&#x5B58;&#x5728;&#x7740;&#x4E00;&#x7CFB;&#x5217;&#x7684;&#x516C;&#x5171;&#x53D8;&#x91CF;</p><table>
<thead>
<tr><th> &#x53D8;&#x91CF;&#x540D; </th>
<th> &#x4F5C;&#x7528; </th>
</tr></thead><tbody>
<tr><td style="text-align:left"> <code>nextHydratableInstance</code> </td>
<td style="text-align:left"> &#x4E0B;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x88AB;<code>hydrate</code>&#x7684;&#x8282;&#x70B9; </td>
</tr><tr><td style="text-align:left"> <code>isHydrating</code> </td>
<td style="text-align:left"> &#x662F;&#x5426;&#x6B63;&#x5728;<code>hydrate</code> </td>
</tr><tr><td style="text-align:left"> <code>hydrationParentFiber</code> </td>
<td style="text-align:left"> &#x4E0B;&#x4E00;&#x4E2A;&#x88AB;<code>hydrate</code>&#x7684;&#x8282;&#x70B9;&#x7684;&#x7236;&#x8282;&#x70B9;</td>
</tr></tbody></table>

<p><code>supportsHydration</code>&#x6765;&#x81EA;&#x4E8E;<code>HostConfig.js</code>&#xFF0C;&#x5728;<code>ReactDOM</code>&#x4E2D;&#xFF0C;&#x4ED6;&#x662F;&#x56FA;&#x5B9A;&#x503C;<code>true</code></p><div class="api-method"><div class="api-method-definition"><h3 id="enterhydrationstate">enterHydrationState</h3><p>&#x5728;&#x66F4;&#x65B0;<code>HostRoot</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x6807;&#x5FD7;&#x7740;&#x5F00;&#x59CB;&#x8FDB;&#x5165;<code>hydrate</code>&#x6D41;&#x7A0B;&#xFF0C;&#x8BBE;&#x7F6E;<code>isHydrating</code>&#x4E3A;<code>true</code></p><p><code>getFirstHydratableChild</code>&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x5B50;&#x8282;&#x70B9;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x666E;&#x901A;<code>Element</code>&#x6216;&#x8005;<code>Text</code>&#x8282;&#x70B9;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">enterHydrationState</span>(<span class="hljs-params">fiber: Fiber</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">if</span> (!supportsHydration) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }

  <span class="hljs-keyword">const</span> parentInstance = fiber.stateNode.containerInfo
  nextHydratableInstance = getFirstHydratableChild(parentInstance)
  hydrationParentFiber = fiber
  isHydrating = <span class="hljs-literal">true</span>
  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getFirstHydratableChild</span>(<span class="hljs-params">
  parentInstance: Container | Instance,
</span>): <span class="hljs-title">null</span> | <span class="hljs-title">Instance</span> | <span class="hljs-title">TextInstance</span> </span>{
  <span class="hljs-keyword">let</span> next = parentInstance.firstChild
  <span class="hljs-keyword">while</span> (
    next &amp;&amp;
    next.nodeType !== ELEMENT_NODE &amp;&amp;
    next.nodeType !== TEXT_NODE
  ) {
    next = next.nextSibling
  }
  <span class="hljs-keyword">return</span> (next: any)
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="trytoclaimnexthydratableinstance">tryToClaimNextHydratableInstance</h1><p>&#x5728;&#x66F4;&#x65B0;&#x6D41;&#x7A0B;&#x4E2D;&#xFF0C;&#x8FD8;&#x9700;&#x8981;&#x505A;&#x7684;&#x5C31;&#x662F;&#x5728;&#x66F4;&#x65B0;<code>HostComponent</code>&#x548C;<code>HostText</code>&#x8282;&#x70B9;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;</p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tryToClaimNextHydratableInstance</span>(<span class="hljs-params">fiber: Fiber</span>): <span class="hljs-title">void</span> </span>{
  <span class="hljs-keyword">if</span> (!isHydrating) {
    <span class="hljs-keyword">return</span>
  }
  <span class="hljs-keyword">let</span> nextInstance = nextHydratableInstance
  <span class="hljs-keyword">if</span> (!nextInstance) {
    <span class="hljs-comment">// Nothing to hydrate. Make it an insertion.</span>
    insertNonHydratedInstance((hydrationParentFiber: any), fiber)
    isHydrating = <span class="hljs-literal">false</span>
    hydrationParentFiber = fiber
    <span class="hljs-keyword">return</span>
  }
  <span class="hljs-keyword">const</span> firstAttemptedInstance = nextInstance
  <span class="hljs-keyword">if</span> (!tryHydrate(fiber, nextInstance)) {
    nextInstance = getNextHydratableSibling(firstAttemptedInstance)
    <span class="hljs-keyword">if</span> (!nextInstance || !tryHydrate(fiber, nextInstance)) {
      <span class="hljs-comment">// Nothing to hydrate. Make it an insertion.</span>
      insertNonHydratedInstance((hydrationParentFiber: any), fiber)
      isHydrating = <span class="hljs-literal">false</span>
      hydrationParentFiber = fiber
      <span class="hljs-keyword">return</span>
    }
    deleteHydratableInstance(
      (hydrationParentFiber: any),
      firstAttemptedInstance,
    )
  }
  hydrationParentFiber = fiber
  nextHydratableInstance = getFirstHydratableChild((nextInstance: any))
}</code></pre>
<p>&#x8BE5;&#x65B9;&#x6CD5;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x662F;&#x68C0;&#x67E5;<code>nextInstance</code>&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x548C;&#x5F53;&#x524D;&#x8282;&#x70B9;&#x8FDB;&#x884C;&#x590D;&#x7528;&#xFF0C;&#x901A;&#x8FC7;<code>tryHydrate</code>&#x8FDB;&#x884C;&#x5224;&#x65AD;</p><p>&#x5224;&#x65AD;&#x6761;&#x4EF6;&#x5176;&#x5B9E;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4E3B;&#x8981;&#x5BF9;&#x6BD4;&#x8282;&#x70B9;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x76F8;&#x540C;&#x3002;</p><p>&#x6CE8;&#x610F;&#x8FD9;&#x91CC;&#x5982;&#x679C;&#x7B2C;&#x4E00;&#x6B21;<code>tryHydrate</code>&#x4E0D;&#x6210;&#x529F;&#xFF0C;&#x4F1A;&#x627E;&#x4ED6;&#x7684;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#x518D;&#x6B21;&#x5C1D;&#x8BD5;&#x4ED6;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x5982;&#x679C;&#x4E24;&#x6B21;&#x90FD;&#x4E0D;&#x6210;&#x529F;&#xFF0C;&#x5C31;&#x76F4;&#x63A5;&#x5173;&#x95ED;&#x6574;&#x4E2A;<code>hydrate</code>&#x6D41;&#x7A0B;&#xFF0C;&#x8BBE;&#x7F6E;<code>isHydrating</code>&#x4E3A;<code>false</code></p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tryHydrate</span>(<span class="hljs-params">fiber, nextInstance</span>) </span>{
  <span class="hljs-keyword">switch</span> (fiber.tag) {
    <span class="hljs-keyword">case</span> HostComponent: {
      <span class="hljs-keyword">const</span> type = fiber.type
      <span class="hljs-keyword">const</span> props = fiber.pendingProps
      <span class="hljs-keyword">const</span> instance = canHydrateInstance(nextInstance, type, props)
      <span class="hljs-keyword">if</span> (instance !== <span class="hljs-literal">null</span>) {
        fiber.stateNode = (instance: Instance)
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
      }
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
    }
    <span class="hljs-keyword">case</span> HostText: {
      <span class="hljs-keyword">const</span> text = fiber.pendingProps
      <span class="hljs-keyword">const</span> textInstance = canHydrateTextInstance(nextInstance, text)
      <span class="hljs-keyword">if</span> (textInstance !== <span class="hljs-literal">null</span>) {
        fiber.stateNode = (textInstance: TextInstance)
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
      }
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
    }
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">canHydrateInstance</span>(<span class="hljs-params">
  instance: Instance | TextInstance,
  type: string,
  props: Props,
</span>): <span class="hljs-title">null</span> | <span class="hljs-title">Instance</span> </span>{
  <span class="hljs-keyword">if</span> (
    instance.nodeType !== ELEMENT_NODE ||
    type.toLowerCase() !== instance.nodeName.toLowerCase()
  ) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
  }
  <span class="hljs-comment">// This has now been refined to an element node.</span>
  <span class="hljs-keyword">return</span> ((instance: any): Instance)
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">canHydrateTextInstance</span>(<span class="hljs-params">
  instance: Instance | TextInstance,
  text: string,
</span>): <span class="hljs-title">null</span> | <span class="hljs-title">TextInstance</span> </span>{
  <span class="hljs-keyword">if</span> (text === <span class="hljs-string">&apos;&apos;</span> || instance.nodeType !== TEXT_NODE) {
    <span class="hljs-comment">// Empty strings are not parsed by HTML so there won&apos;t be a correct match here.</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>
  }
  <span class="hljs-comment">// This has now been refined to a text node.</span>
  <span class="hljs-keyword">return</span> ((instance: any): TextInstance)
}</code></pre>
</div></div></div>



<h1 id="&#x771F;&#x6B63;&#x7684;hydrate">&#x771F;&#x6B63;&#x7684;<code>hydrate</code></h1><p>&#x4E4B;&#x524D;&#x662F;&#x5728;&#x66F4;&#x65B0;&#x8282;&#x70B9;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x901A;&#x8FC7;&#x5224;&#x65AD;&#x6765;&#x786E;&#x5B9A;&#x662F;&#x5426;&#x53EF;&#x4EE5;<code>hydrate</code>&#x7684;&#x8FC7;&#x7A0B;&#xFF0C;&#x771F;&#x6B63;&#x7684;&#x5408;&#x5E76;&#x8981;&#x5728;<code>completeWork</code>&#x4E2D;&#x8FDB;&#x884C;&#xFF0C;&#x56E0;&#x4E3A; DOM &#x8282;&#x70B9;&#x662F;&#x5728;&#x8FD9;&#x91CC;&#x88AB;&#x521B;&#x5EFA;&#x7684;&#x3002;</p><pre><code class="lang-js"><span class="hljs-keyword">let</span> wasHydrated = popHydrationState(workInProgress);
<span class="hljs-keyword">if</span> (wasHydrated) {
  <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Move this and createInstance step into the beginPhase</span>
  <span class="hljs-comment">// to consolidate.</span>
  <span class="hljs-keyword">if</span> (
    prepareToHydrateHostInstance(
      workInProgress,
      rootContainerInstance,
      currentHostContext,
    )
  ) {
    <span class="hljs-comment">// If changes to the hydrated node needs to be applied at the</span>
    <span class="hljs-comment">// commit-phase we mark this as such.</span>
    markUpdate(workInProgress);
  }</code></pre>
<div class="api-method"><div class="api-method-definition"><h3 id="pophydrationstate">popHydrationState</h3><p>&#x5728;<code>completeUnitOfWork</code>&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5BF9;&#x4E8E;<code>HostComponent</code>&#x548C;<code>HostText</code>&#x4F1A;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x3002;</p><p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x4E00;&#x4E2A;&#x903B;&#x8F91;&#xFF0C;&#x5C31;&#x662F;&#x5728;<code>update</code>&#x8282;&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x6CBF;&#x7740;&#x4E00;&#x4FA7;&#x5B50;&#x6811;&#x5411;&#x4E0B;&#x904D;&#x5386;&#x5230;&#x53F6;&#x5B50;&#x8282;&#x70B9;&#xFF0C;&#x90A3;&#x4E48;<code>hydrationParentFiber</code>&#x4F1A;&#x7B49;&#x4E8E;&#x53F6;&#x5B50;&#x8282;&#x70B9;&#x5BF9;&#x5E94;&#x7684; DOM &#x8282;&#x70B9;&#xFF0C;&#x800C;<code>nextHydratableInstance</code>&#x4F1A;&#x7B49;&#x4E8E;&#x4ED6;&#x7684;&#x5B50;&#x8282;&#x70B9;&#x3002;&#x800C;<code>completeUnitOfWork</code>&#x662F;&#x4ECE;&#x8FD9;&#x4E2A;&#x53F6;&#x5B50;&#x8282;&#x70B9;&#x5F00;&#x59CB;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5462;&#x8FD9;&#x8FB9;&#x7684;&#x5224;&#x65AD;<code>fiber !== hydrationParentFiber</code>&#x6B63;&#x5E38;&#x6765;&#x8BB2;&#x662F;&#x4E0D;&#x6210;&#x7ACB;&#x7684;&#x3002;</p><p>&#x800C;&#x5728;&#x4E0B;&#x4E00;&#x4E2A;&#x5224;&#x65AD;&#x4E2D;&#xFF0C;&#x5BF9;&#x4E8E;&#x5F53;&#x524D;<code>fiber</code>&#x662F;&#x6587;&#x5B57;&#x8282;&#x70B9;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x4F1A;&#x6E05;&#x9664;<code>nextHydratableInstance</code>&#x548C;&#x4ED6;&#x7684;&#x6240;&#x6709;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x6587;&#x5B57;&#x8282;&#x70B9;&#x4E0D;&#x4F1A;&#x6709;&#x5B50;&#x8282;&#x70B9;&#x3002;</p><p><code>popToNextHostParent</code>&#x8FD9;&#x5C31;&#x662F;&#x627E;&#x7236;&#x94FE;&#x4E0A;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;<code>HostComponent</code>&#x8282;&#x70B9;&#x4E86;&#x3002;</p><p>&#x6700;&#x540E;&#x8BBE;&#x7F6E;<code>nextHydratableInstance</code>&#x4E3A;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x56E0;&#x4E3A;&#x6309;&#x7167; React &#x904D;&#x5386;<code>Fiber</code>&#x6811;&#x7684;&#x6D41;&#x7A0B;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#xFF0C;&#x63A5;&#x4E0B;&#x53BB;&#x4F1A;&#x66F4;&#x65B0;&#x5144;&#x5F1F;&#x8282;&#x70B9;&#x3002;</p><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">popToNextHostParent</span>(<span class="hljs-params">fiber: Fiber</span>): <span class="hljs-title">void</span> </span>{
  <span class="hljs-keyword">let</span> parent = fiber.return
  <span class="hljs-keyword">while</span> (
    parent !== <span class="hljs-literal">null</span> &amp;&amp;
    parent.tag !== HostComponent &amp;&amp;
    parent.tag !== HostRoot
  ) {
    parent = parent.return
  }
  hydrationParentFiber = parent
}</code></pre>
</div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">popHydrationState</span>(<span class="hljs-params">fiber: Fiber</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">if</span> (!supportsHydration) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }
  <span class="hljs-keyword">if</span> (fiber !== hydrationParentFiber) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }
  <span class="hljs-keyword">if</span> (!isHydrating) {
    popToNextHostParent(fiber)
    isHydrating = <span class="hljs-literal">true</span>
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
  }

  <span class="hljs-keyword">const</span> type = fiber.type

  <span class="hljs-keyword">if</span> (
    fiber.tag !== HostComponent ||
    (type !== <span class="hljs-string">&apos;head&apos;</span> &amp;&amp;
      type !== <span class="hljs-string">&apos;body&apos;</span> &amp;&amp;
      !shouldSetTextContent(type, fiber.memoizedProps))
  ) {
    <span class="hljs-keyword">let</span> nextInstance = nextHydratableInstance
    <span class="hljs-keyword">while</span> (nextInstance) {
      deleteHydratableInstance(fiber, nextInstance)
      nextInstance = getNextHydratableSibling(nextInstance)
    }
  }

  popToNextHostParent(fiber)
  nextHydratableInstance = hydrationParentFiber
    ? getNextHydratableSibling(fiber.stateNode)
    : <span class="hljs-literal">null</span>
  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="preparetohydratehostinstance">prepareToHydrateHostInstance</h1><p>&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5F88;&#x50CF;<code>completeWork</code>&#x4E2D;&#x5BF9;&#x4E8E;<code>HostComponent</code>&#x7684;<code>updateHostComponent</code>&#xFF0C;&#x8C03;&#x7528;<code>hydrateInstance</code>&#x5E76;&#x8FD4;&#x56DE;<code>payload</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7C7B;&#x4F3C;&#x4E8E;<code>finalizeInitialChildren</code>&#x548C;<code>diffProperties</code>&#x7684;&#x7ED3;&#x5408;&#xFF0C;&#x6267;&#x884C;&#x4E86;&#x4E8B;&#x4EF6;&#x7684;&#x521D;&#x59CB;&#x5316;&#x7ED1;&#x5B9A;&#xFF0C;&#x5E76;&#x5BF9;&#x6BD4;&#x5C5E;&#x6027;&#x53D8;&#x5316;&#x5E76;&#x8FD4;&#x56DE;<code>updatePayload</code>&#x3002;&#x5177;&#x4F53;&#x5185;&#x5BB9;&#x5C31;&#x4E0D;&#x6DF1;&#x7A76;&#x4E86;&#xFF0C;&#x6709;&#x5174;&#x8DA3;&#x7684;&#x540C;&#x5B66;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x53BB;&#x770B;&#x3002;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">prepareToHydrateHostInstance</span>(<span class="hljs-params">
  fiber: Fiber,
  rootContainerInstance: Container,
  hostContext: HostContext,
</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> instance: Instance = fiber.stateNode
  <span class="hljs-keyword">const</span> updatePayload = hydrateInstance(
    instance,
    fiber.type,
    fiber.memoizedProps,
    rootContainerInstance,
    hostContext,
    fiber,
  )
  fiber.updateQueue = (updatePayload: any)
  <span class="hljs-keyword">if</span> (updatePayload !== <span class="hljs-literal">null</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
  }
  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hydrateInstance</span>(<span class="hljs-params">
  instance: Instance,
  type: string,
  props: Props,
  rootContainerInstance: Container,
  hostContext: HostContext,
  internalInstanceHandle: Object,
</span>): <span class="hljs-title">null</span> | <span class="hljs-title">Array</span>&lt;<span class="hljs-title">mixed</span>&gt; </span>{
  precacheFiberNode(internalInstanceHandle, instance)
  updateFiberProps(instance, props)
  <span class="hljs-keyword">let</span> parentNamespace: string
  parentNamespace = ((hostContext: any): HostContextProd)
  <span class="hljs-keyword">return</span> diffHydratedProperties(
    instance,
    type,
    props,
    parentNamespace,
    rootContainerInstance,
  )
}</code></pre>
</div></div></div>



<div class="api-method"><div class="api-method-definition"><h1 id="preparetohydratehosttextinstance">prepareToHydrateHostTextInstance</h1><p>&#x5BF9;&#x4E8E;&#x6587;&#x5B57;&#x8282;&#x70B9;&#x5224;&#x65AD;&#x662F;&#x5426;&#x9700;&#x8981;&#x66F4;&#x65B0;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">prepareToHydrateHostTextInstance</span>(<span class="hljs-params">fiber: Fiber</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> textInstance: TextInstance = fiber.stateNode
  <span class="hljs-keyword">const</span> textContent: string = fiber.memoizedProps
  <span class="hljs-keyword">const</span> shouldUpdate = hydrateTextInstance(textInstance, textContent, fiber)
  <span class="hljs-keyword">return</span> shouldUpdate
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hydrateTextInstance</span>(<span class="hljs-params">
  textInstance: TextInstance,
  text: string,
  internalInstanceHandle: Object,
</span>): <span class="hljs-title">boolean</span> </span>{
  precacheFiberNode(internalInstanceHandle, textInstance)
  <span class="hljs-keyword">return</span> diffHydratedText(textInstance, text)
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">diffHydratedText</span>(<span class="hljs-params">textNode: Text, text: string</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> isDifferent = textNode.nodeValue !== text
  <span class="hljs-keyword">return</span> isDifferent
}</code></pre>
</div></div></div>



<h1 id="&#x5220;&#x9664;&#x591A;&#x4F59;&#x7684;-dom-&#x8282;&#x70B9;">&#x5220;&#x9664;&#x591A;&#x4F59;&#x7684; DOM &#x8282;&#x70B9;</h1><div class="api-method"><div class="api-method-definition"><h1 id="deletehydratableinstance">deleteHydratableInstance</h1><p>&#x6700;&#x7EC8;&#x7684;&#x5220;&#x9664;&#x8981;&#x653E;&#x5230;<code>commit</code>&#x9636;&#x6BB5;&#x53BB;&#x505A;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x4E13;&#x95E8;&#x4E3A;&#x4E86;&#x8BB0;&#x5F55;&#x5220;&#x9664;&#x884C;&#x4E3A;&#x521B;&#x5EFA;&#x4E86;<code>Fiber</code>&#x5BF9;&#x8C61;&#xFF0C;&#x7528;&#x4E8E;&#x8BB0;&#x5F55;<code>side effect</code>&#xFF0C;&#x5E76;&#x52A0;&#x5165;&#x5230;&#x4E86;<code>effect</code>&#x94FE;&#x4E0A;&#x3002;</p></div><div class="api-method-code"><div class="api-method-sample" data-lang="js" data-name="JavaScript"><pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">deleteHydratableInstance</span>(<span class="hljs-params">
  returnFiber: Fiber,
  instance: HydratableInstance,
</span>) </span>{
  <span class="hljs-keyword">const</span> childToDelete = createFiberFromHostInstanceForDeletion()
  childToDelete.stateNode = instance
  childToDelete.return = returnFiber
  childToDelete.effectTag = Deletion

  <span class="hljs-keyword">if</span> (returnFiber.lastEffect !== <span class="hljs-literal">null</span>) {
    returnFiber.lastEffect.nextEffect = childToDelete
    returnFiber.lastEffect = childToDelete
  } <span class="hljs-keyword">else</span> {
    returnFiber.firstEffect = returnFiber.lastEffect = childToDelete
  }
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createFiberFromHostInstanceForDeletion</span>(<span class="hljs-params"></span>): <span class="hljs-title">Fiber</span> </span>{
  <span class="hljs-keyword">const</span> fiber = createFiber(HostComponent, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, NoContext)
  <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> These should not need a type.</span>
  fiber.elementType = <span class="hljs-string">&apos;DELETED&apos;</span>
  fiber.type = <span class="hljs-string">&apos;DELETED&apos;</span>
  <span class="hljs-keyword">return</span> fiber
}</code></pre>
</div></div></div>




                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
<div id="disqus_thread"></div>
<script>
  /**
   *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
   *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables*/
  /*
var disqus_config = function () {
this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
*/
  ;(function() {
    // DON'T EDIT BELOW THIS LINE
    var d = document,
      s = d.createElement('script')
    s.src = 'https://react-source-code.disqus.com/embed.js'
    s.setAttribute('data-timestamp', +new Date())
    ;(d.head || d.body).appendChild(s)
  })()
</script>
<noscript
  >Please enable JavaScript to view the
  <a href="https://disqus.com/?ref_noscript"
    >comments powered by Disqus.</a
  ></noscript
>

            </div>

            

        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"hydrate","level":"1.6.2","depth":2,"next":{"title":"ref","level":"1.6.3","depth":2,"path":"book/features/ref.md","ref":"book/features/ref.md","articles":[]},"previous":{"title":"context","level":"1.6.1","depth":2,"path":"book/features/context.md","ref":"book/features/context.md","articles":[]},"dir":"ltr"},"config":{"plugins":["theme-api"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["weibo","google","twitter","weibo","instapaper"]},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-api":{"languages":[],"split":true,"theme":"light"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React源码解析","language":"zh-hans","gitbook":"*"},"file":{"path":"book/features/hydrate.md","mtime":"2019-01-07T03:56:29.827Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2019-07-08T12:28:32.264Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-api/theme-api.js"></script>
        
    

    </body>
</html>

